package com.davidk.survey.client.menu;

import com.smartgwt.client.data.Criteria;
import com.smartgwt.client.types.ContentsType;
import com.smartgwt.client.types.GroupStartOpen;
import com.smartgwt.client.types.Overflow;
import com.smartgwt.client.types.SelectionStyle;
import com.smartgwt.client.widgets.Canvas;
import com.smartgwt.client.widgets.HTMLPane;
import com.smartgwt.client.widgets.form.DynamicForm;
import com.smartgwt.client.widgets.form.fields.PickerIcon;
import com.smartgwt.client.widgets.form.fields.TextItem;
import com.smartgwt.client.widgets.form.fields.events.FormItemClickHandler;
import com.smartgwt.client.widgets.form.fields.events.FormItemIconClickEvent;
import com.smartgwt.client.widgets.form.fields.events.KeyPressEvent;
import com.smartgwt.client.widgets.form.fields.events.KeyPressHandler;
import com.smartgwt.client.widgets.grid.ListGrid;
import com.smartgwt.client.widgets.grid.events.DataArrivedEvent;
import com.smartgwt.client.widgets.grid.events.DataArrivedHandler;
import com.smartgwt.client.widgets.layout.VLayout;

public class FunctionMenuView extends VLayout implements FunctionMenuViewInterface {

	final ListGrid grid = new ListGrid();
	TextItem query = new TextItem();
	DynamicForm form = new DynamicForm();
	Criteria userCriteria = null;
	HTMLPane htmlView = new HTMLPane();

	public Canvas getView() {
		return this;
	}

	public FunctionMenuView() {
		final VLayout layout = this;
		layout.setWidth(200);
		layout.setTitle("BPM Menu");
		FunctionMenuDataSource employeeDS = FunctionMenuDataSource.getInstance();

		query.setName("Name");
		query.setShowTitle(false);
		query.addKeyPressHandler(new KeyPressHandler() {

			@Override
			public void onKeyPress(KeyPressEvent event) {
				if (event.getKeyName().equals("Enter"))
					refreshMenu();
			}

		});

		PickerIcon searchPicker = new PickerIcon(PickerIcon.SEARCH, new FormItemClickHandler() {
			public void onFormItemClick(FormItemIconClickEvent event) {
				refreshMenu();
			}
		});
		query.setIcons(searchPicker);
		form.setFields(query);
		layout.addMember(form);

		grid.setDataSource(employeeDS);
		grid.setShowHeader(false);
		// grid.setGroupByField("ParentId");
		grid.setShowAllRecords(true);
		grid.setGroupStartOpen(GroupStartOpen.ALL);
		grid.setSelectionType(SelectionStyle.SINGLE);
		grid.setShowAllRecords(true);
		grid.setBorder("0px");
		grid.setBodyOverflow(Overflow.VISIBLE);
		grid.setOverflow(Overflow.VISIBLE);
		grid.addDataArrivedHandler(new DataArrivedHandler() {
			@Override
			public void onDataArrived(DataArrivedEvent event) {
				if (grid.isGrouped())
					grid.getGroupTree().openAll();
			}
		});
		layout.addMember(grid);

		htmlView.setContentsType(ContentsType.PAGE);
		htmlView.setScrollbarSize(0);
	}

	public void refreshMenu() {
		grid.invalidateCache();
		Criteria c = form.getValuesAsCriteria();
		c.addCriteria(userCriteria);
		grid.filterData(c);
	}

	public ListGrid getGrid() {
		return grid;
	}

	@Override
	public void fetchData(String userid) {
		userCriteria = new Criteria("userid", userid);
		grid.fetchData(userCriteria);
	}

	@Override
	public Canvas getHtmlView(String url) {
		htmlView.setContentsURL(url);
		return htmlView;
	}
}
